 <template>
   <el-card shadow="always" class="box-card">
      <div class="card-name">
        <div class="name width2">应收管理</div>
      </div>
      <div class="content-con">
        <el-form :model="form" ref="form" size="medium" label-width="80px">
          <el-row class="flex-s">
            <el-form-item label="客户:" prop="client">
              <el-input v-model="form.client" clearable placeholder="客户">
                <el-button size="mini" slot="append" icon="el-icon-search" @click="open"></el-button>
              </el-input>
            </el-form-item>
            <el-form-item label="日期:">
              <el-date-picker
                v-model="form.date"
                type="daterange"
                range-separator="——"
                start-placeholder="开始日期"
                end-placeholder="结束日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button class="save" @click="search">查 询</el-button>
            </el-form-item>
          </el-row>
        </el-form>

        <div class="bank-form">
          <div class="form-item">
            <el-table
              :data="tableData.slice((currentPage-1)*pagesize,currentPage*pagesize)"
              class="table"
              stripe border
              show-summary
              :summary-method="getSummaries"
              :header-cell-style="{background:'#f3f3f3',textAlign: 'center'}"
              :cell-style="{ textAlign: 'center' }">
              <el-table-column prop="client" label="客户" width="120">
              </el-table-column>
              <el-table-column prop="sx_date" label="销售日期" width="140">
              </el-table-column>
              <el-table-column prop="sale_no" label="销售单据号" width="160">
              </el-table-column>
              <el-table-column prop="start_ar" label="期初应收余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="start_aback" label="期初应退余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="add_ar" label="本期应收金额增加(元)" width="120">
              </el-table-column>
              <el-table-column prop="sub_ar" label="本期应收金额减少(元)" width="120">
              </el-table-column>
              <el-table-column prop="in_ar" label="本期应收金额余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="add_aback" label="本期应退金额增加(元)" width="120">
              </el-table-column>
              <el-table-column prop="sub_aback" label="本期应退金额减少(元)" width="120">
              </el-table-column>
              <el-table-column prop="in_aback" label="本期应退金额余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="end_ar" label="期末应收余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="end_aback" label="期末应退余额(元)" width="120">
              </el-table-column>
              <el-table-column prop="ar" label="账目余额(元)" width="160">
              </el-table-column>
            </el-table>
          </div>
          <!-- 分页-页码 -->
          <div class="page">
            <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="currentPage"
              :page-sizes="[5, 10, 15, 20]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="tableData.length">
            </el-pagination>
          </div>
        </div>
      </div>
      <modal-client ref="ModalClient"></modal-client>
   </el-card>

 </template>
<script>
import ModalClient from '@/components/client-manage/ModalClient';
export default {
  components: {
    ModalClient
  },
  data () {
    return {
      currentPage: 1, //当前页码
      pagesize: 5,    //每页的数据条数
      tableData: [
        {
          client: '小花',
          sx_date: '2020-11-18',
          sale_no: 'XS202011181609499',
          start_ar: '700',
          start_aback: '0',
          add_ar: '0',
          sub_ar: '0',
          in_ar: '0',
          add_aback: '0',
          sub_aback: '0',
          in_aback: '0',
          end_ar: '700',
          end_aback: '0',
          ar: '700'
        }
      ],
      form: {
        client: '',
        date: ''
      }
    }
  },

  methods: {
    //搜索
    search() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          console.log(this.form)
          this.$message.success('成功!');
        } else {
          console.log('error submit!!');
          this.$message.error('失败!');
          return false;
        }
      });
    },

    //客户-对话框
    open() {
      this.$nextTick(()=>{
        this.$refs.ModalClient.openClients();
      });
    },

    //表格合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = []; //声明变量
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计：';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if(
          column.property == "ar"
        ) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
        } else {
        }
      });
      return sums;
    },

    //每页条数改变时触发事件,选择一页显示多少行
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.currentPage = 1;
      this.pagesize = val;
    },
    // 当前页改变时触发 跳转其他页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
    }
  }
}
</script>
<style lang="scss">

</style>
